package com.qfedu.service.impl;

import com.qfedu.entity.ResultVo;
import com.qfedu.entity.UserHobby;
import com.qfedu.mapper.ProductMapper;
import com.qfedu.mapper.UserHobbyMapper;
import com.qfedu.service.UserHobbyService;
import com.qfedu.util.JsonUtil;
import com.qfedu.vo.ProductVo;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * 作者：刘随阳
 * 时间: 2021/9/25 16:04
 * 描述：神光照耀,太白成瑞。六丙来迎,百福悠归
 */
@Service
public class UserHobbyServiceImpl implements UserHobbyService {

    @Autowired
    private UserHobbyMapper userHobbyMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public ResultVo updateUserHobby(UserHobby userHobby) {
        Example example= new Example(UserHobby.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId",userHobby.getUserId());
        criteria.andEqualTo("itemId",userHobby.getItemId());
        List<UserHobby> userHobbies = userHobbyMapper.selectByExample(example);
        if(userHobbies.size() == 0){
            userHobbyMapper.insert(userHobby);
            return ResultVo.success("添加成功");
        }
        if (userHobbies.get(0).getScore() >= userHobby.getScore()){
            return  ResultVo.success("用户偏好已存在");
        }else {
            userHobby.setUserHobbyId(userHobbies.get(0).getUserHobbyId());
            int i = userHobbyMapper.updateByPrimaryKey(userHobby);

            return ResultVo.success("修改成功");
        }
    }

    @Override
    public ResultVo getByUserCF(String  token) {
        List<Integer> products = new ArrayList<>();
        if(token.equals("123")){
            products.add(1);
            products.add(2);
            products.add(3);
            products.add(4);
            products.add(5);
        }else {
            JwtParser parser = Jwts.parser();
            parser.setSigningKey("123456");
            Jws<Claims> claimsJws = parser.parseClaimsJws(token);
            Claims body = claimsJws.getBody();
            String id = body.getId();
            Integer userId = Integer.parseInt(id);
            String productJson = (String) stringRedisTemplate.opsForHash().get("UserCF",userId.toString());
            products = JsonUtil.stringToList(productJson, Integer.class);
            if(products.size() == 0){
//                return ResultVo.fail("尚未有推荐");
                List<ProductVo> productVos = new ArrayList<>();
                for (Integer product : products) {
                    ProductVo productVo= productMapper.getProductVo(product);
                    productVos.add(productVo);
                }

                return ResultVo.success("查询成功",productVos);
            }
        }

        List<ProductVo> productVos = new ArrayList<>();
        for (Integer product : products) {
            ProductVo productVo= productMapper.getProductVo(product);
            productVos.add(productVo);
        }

        return ResultVo.success("查询成功",productVos);
    }

    @Override
    public ResultVo getByItemCF(String token) {
        List<Integer> products = new ArrayList<>();
        if(token.equals("123")){
            products.add(1);
            products.add(2);
            products.add(3);
            products.add(4);
            products.add(5);
        }else {
            JwtParser parser = Jwts.parser();
            parser.setSigningKey("123456");
            Jws<Claims> claimsJws = parser.parseClaimsJws(token);
            Claims body = claimsJws.getBody();
            String id = body.getId();
            Integer userId = Integer.parseInt(id);
            String productJson = (String) stringRedisTemplate.opsForHash().get("ItemCF",userId.toString());
            products = JsonUtil.stringToList(productJson, Integer.class);
            if(products.size() == 0){
                return ResultVo.fail("尚未有推荐");
            }
        }
        List<ProductVo> productVos = new ArrayList<>();
        for (Integer product : products) {
            ProductVo productVo= productMapper.getProductVo(product);
            productVos.add(productVo);
        }

        return ResultVo.success("查询成功",productVos);
    }
}
